package com.kaili.basic.controlle;


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.CustomerAddDto;
import com.kaili.basic.pojo.dto.CustomerUpdateDto;
import com.kaili.basic.pojo.dto.CustomerQueryDto;
import com.kaili.basic.pojo.entity.*;
import com.kaili.basic.service.CustomerAddressService;
import com.kaili.basic.service.CustomerService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 客户管理 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2019-03-07
 */
@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "客户管理", description = "客户管理相关API")
public class CustomerController {
    @Autowired
    CustomerService customerService;
    @Autowired
    CustomerAddressService customerAddressService;

    //新增客户
    @PostMapping(value = "/customers/addCustomer")
    @ApiOperation(value = "新增客户",notes = "新增客户")
    public RestStatus addCustomer(@RequestBody CustomerAddDto customerAddDto){
        customerService.addCustomer(customerAddDto);
        return CommonStatus.ADD_OK;
    }

    //修改客户
    @PutMapping(value = "/customers/updateCustomer")
    @ApiOperation(value = "修改客户",notes = "修改客户")
    public RestStatus updateCustomer(@RequestBody CustomerUpdateDto customerUpdateDto){
        customerService.updateCustomer(customerUpdateDto);
        return CommonStatus.UPDATE_OK;
    }

    @DeleteMapping("/customers/{customerIds}/deleteCustomers")
    @ApiOperation(value = "删除客户", notes = "删除客户")
    public RestStatus deleteCustomers(@PathVariable Long[] customerIds) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("modifyBy",LoginDetailUtils.getCurrentEmployeeId().longValue());
        map.put("customerIds",customerIds);
        customerService.deleteCustomers(map);
        return CommonStatus.DELETE_OK;
    }

    //条件查询获取客户信息
    @GetMapping(value = "/customers/queryCustomer")
    @ApiOperation(value = "条件查询客户信息",notes = "条件查询客户信息")
    public Page<CustomerPlusAddress> queryCustomer(@ModelAttribute CustomerQueryDto customerQueryDto) {
        CustomerPlusAddress customer = new CustomerPlusAddress();
        BeanUtils.copyProperties(customerQueryDto, customer);
        return customerService.queryCustomer(new Page<CustomerPlusAddress>(customer.getPage(),customer.getPageSize()),customer);
    }

    //根据id查询客户信息
    @GetMapping(value = "/customers/{customerId}/queryCustomerById")
    @ApiOperation(value = "根据id查询客户信息",notes = "根据id查询客户信息")
    public Customer queryCustomerById(@PathVariable("customerId") Long customerId) {
        return customerService.queryCustomerById(customerId);
    }

    @GetMapping(value = "/customers/{supplierId}/queryCustomerBySupplier")
    @ApiOperation(value = "根据供应商id获取客户详细信息",notes = "根据供应商id获取客户详细信息")
    public List<Customer> queryCustomerBySupplier(@PathVariable("supplierId") Long supplierId) {
        return customerService.queryCustomerBySupplier(supplierId);
    }

    @GetMapping(value = "/customers/dimQueryCustomerBySupplier")
    @ApiOperation(value = "根据供应商id模糊查询客户详细信息",notes = "根据供应商id模糊查询客户详细信息")
    public List<Customer> dimQueryCustomerBySupplier(@RequestParam("supplierId") Long supplierId,@RequestParam("parameterName") String parameterName) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("supplierId",supplierId);
        map.put("parameterName",parameterName);
        return customerService.dimQueryCustomerBySupplier(map);
    }

    //查询所有客户（下拉框使用）
    @GetMapping(value = "/customers/queryAllCustomer")
    @ApiOperation(value = "查询所有客户（下拉框使用）",notes = "查询所有客户（下拉框使用）")
    public List<CustomerAllQuery> queryAllCustomer() {
        return customerService.queryAllCustomer();
    }

    //根据id查询客户信息
    @GetMapping(value = "/customers/{customerId}/queryLinkmanByCustomerId")
    @ApiOperation(value = "根据客户id查询客户联系人地址信息",notes = "根据客户id查询客户联系人地址信息")
    public List<CustomerAddress> queryLinkmanByCustomerId(@PathVariable("customerId") Long customerId) {
        return customerAddressService.getCustomerAddressList(customerId);
    }

    @GetMapping("/customers/export")
    @ApiOperation(value = "导出客户信息")
    public void export(HttpServletResponse response, @ModelAttribute CustomerQueryDto customerQueryDto) throws RestException {
        List<CustomerExport> list = customerService.queryCustomerExport(customerQueryDto);
        ExcelUtil.exportExcel(list,"客户信息","sheet1", CustomerExport.class, UUID.randomUUID().toString()+".xls",response);
    }
}

